﻿using System;
using System.Linq;

namespace DNUtil
{
    public static class DateTimeUtil
    {
        /// <summary>
        /// Rounds a DateTime value up on midpoint.
        /// </summary>
        /// <param name="value">DateTime value to round off</param>
        /// <param name="span">Timespan value specifying the interval to round off to</param>
        /// <returns></returns>
        public static DateTime Round(this DateTime value, TimeSpan span)
        {
            long ticks = (value.Ticks + (span.Ticks / 2) + 1) / span.Ticks;
            return new DateTime(ticks * span.Ticks, value.Kind);
        }

        public static DateTime Floor(this DateTime date, TimeSpan span)
        {
            long ticks = (date.Ticks / span.Ticks);
            return new DateTime(ticks * span.Ticks);
        }

        public static DateTime Ceil(this DateTime date, TimeSpan span)
        {
            long ticks = (date.Ticks + span.Ticks - 1) / span.Ticks;
            return new DateTime(ticks * span.Ticks);
        }
    }
}
